if (window.DAIJH == null) {
  window.DAIJH = {};
}
DAIJH.author = "DAIJH";
DAIJH.version = "1.0.0";

(function (jq) {
  // 基礎輔助函數
  jq.extend(DAIJH, {
    _isNull: function (v) {
      return v == undefined || v == null;
    },
    _isExist: function (o) {
      if (typeof (o) == typeof (jq())) {
        return o.length > 0;
      }
    },
    _hasProperty: function (o, k, d) {
      return o.hasOwnProperty(k) ? o[k] : (d || false);
    },
    _randomId: function (l) {
      let len = l || 8;
      const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
      const maxPos = $chars.length;
      let id = '';
      for (let i = 0; i < len; i++) {
        id += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return id;
    },
    _num2px: function (n) {
      return n + "px";
    },
    _assembleElements: function (o) {
      if (o.node) {
        o.pchild?.forEach((e, i) => {
          DAIJH._assembleElements(e);
          o.node.prepend(e.node);
        });
        o.achild?.forEach((e, i) => {
          DAIJH._assembleElements(e);
          o.node.append(e.node);
        });
      }
    },
    throw: function (e) {
      return console.error(e);
    },
    ERROR: {
      E10001: function (e) {
        return e + "函數參數未傳遞！";
      }
    }
  });

  // SVG 圖標定義
  jq.extend(DAIJH, {
    ICON: {
      success: '<svg viewBox="0 0 1024 1024"xmlns="http://www.w3.org/2000/svg"><path fill="currentColor"d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm-55.808 536.384-99.52-99.584a38.4 38.4 0 1 0-54.336 54.336l126.72 126.72a38.272 38.272 0 0 0 54.336 0l262.4-262.464a38.4 38.4 0 1 0-54.272-54.336L456.192 600.384z"></path></svg>',
      warning: '<svg viewBox="0 0 1024 1024"xmlns="http://www.w3.org/2000/svg"><path fill="currentColor"d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm0 192a58.432 58.432 0 0 0-58.24 63.744l23.36 256.384a35.072 35.072 0 0 0 69.76 0l23.296-256.384A58.432 58.432 0 0 0 512 256zm0 512a51.2 51.2 0 1 0 0-102.4 51.2 51.2 0 0 0 0 102.4z"></path></svg>',
      info: '<svg viewBox="0 0 1024 1024"xmlns="http://www.w3.org/2000/svg"><path fill="currentColor"d="M512 64a448 448 0 1 1 0 896.064A448 448 0 0 1 512 64zm67.2 275.072c33.28 0 60.288-23.104 60.288-57.344s-27.072-57.344-60.288-57.344c-33.28 0-60.16 23.104-60.16 57.344s26.88 57.344 60.16 57.344zM590.912 699.2c0-6.848 2.368-24.64 1.024-34.752l-52.608 60.544c-10.88 11.456-24.512 19.392-30.912 17.28a12.992 12.992 0 0 1-8.256-14.72l87.68-276.992c7.168-35.136-12.544-67.2-54.336-71.296-44.096 0-108.992 44.736-148.48 101.504 0 6.784-1.28 23.68.064 33.792l52.544-60.608c10.88-11.328 23.552-19.328 29.952-17.152a12.8 12.8 0 0 1 7.808 16.128L388.48 728.576c-10.048 32.256 8.96 63.872 55.04 71.04 67.84 0 107.904-43.648 147.456-100.416z"></path></svg>',
      error: '<svg viewBox="0 0 1024 1024"xmlns="http://www.w3.org/2000/svg"><path fill="currentColor"d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm0 393.664L407.936 353.6a38.4 38.4 0 1 0-54.336 54.336L457.664 512 353.6 616.064a38.4 38.4 0 1 0 54.336 54.336L512 566.336 616.064 670.4a38.4 38.4 0 1 0 54.336-54.336L566.336 512 670.4 407.936a38.4 38.4 0 1 0-54.336-54.336L512 457.664z"></path></svg>',
      close: '<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path fill = "currentColor" d="M764.288 214.592 512 466.88 259.712 214.592a31.936 31.936 0 0 0-45.12 45.12L466.752 512 214.528 764.224a31.936 31.936 0 1 0 45.12 45.184L512 557.184l252.288 252.288a31.936 31.936 0 0 0 45.12-45.12L557.12 512.064l252.288-252.352a31.936 31.936 0 1 0-45.12-45.184z" ></path></svg >',
    }
  });

  // Feedback 模塊
  jq.extend(DAIJH, {
    Feedback: {
      notification: function (o) {
        if (DAIJH._isNull(o)) {
          DAIJH.throw(DAIJH.ERROR.E10001("DAIJH.Feedback.notification()"));
          return;
        }

        const title = DAIJH._hasProperty(o, "title") || "提示";
        const message = DAIJH._hasProperty(o, "message") || "";
        const type = DAIJH._hasProperty(o, "type") || "info";
        const duration = DAIJH._hasProperty(o, "duration") || 4500;
        const showClose = DAIJH._hasProperty(o, "showClose", true);
        const callback = DAIJH._hasProperty(o, "callback");
        const position = DAIJH._hasProperty(o, "position") || "right-top";

        const [positionX, positionY] = position.split("-");
        const idStr = "daijh_notification_" + position.replace("-", "_") + "_";
        const id = idStr + DAIJH._randomId(8);
        let positionOffset = 16;

        jq("div[id^='" + idStr + "']").each((i, el) => { positionOffset += parseInt(jq(el).outerHeight(), 10) + 16 });
        const positionStyle = { [positionY]: DAIJH._num2px(positionOffset), [positionX]: "-330px", opacity: 0 };
        const positionShowStyle = { [positionX]: "16px", opacity: 1 };

        const $dv = jq("<div id='" + id + "'></div>").addClass("daijh-notification").addClass("daijh-notification--" + type).css({ "z-index": 9999, ...positionStyle });

        const $notification_group = jq("<div></div>").addClass("daijh-notification__group");
        const $notification_title = jq("<h2>" + title + "</h2>").addClass("daijh-notification__title");
        const $notification_content = jq("<div>" + message + "</div>").addClass("daijh-notification__content");

        if (duration > 0) {
          $dv.addClass('has-progress-bar');
          $dv.css('--daijh-notification-duration', duration + 'ms');
        }

        if (type !== "") {
          const $notification__icon_svg = jq(DAIJH.ICON[type] || DAIJH.ICON.info);
          const $notification__icon = jq("<i></i>").addClass("daijh-icon daijh-notification__icon").addClass("daijh-notification--" + type);
          DAIJH._assembleElements({
            node: $dv,
            achild: [{
              node: $notification__icon,
              achild: [{ node: $notification__icon_svg }]
            }]
          });
        }

        if (showClose) {
          const $notification_closeBtn = jq("<i></i>").addClass("daijh-icon daijh-notification__closeBtn");
          const $notification__closeBtn_icon = jq(DAIJH.ICON.close);
          $notification_closeBtn.click(() => { removeDv(); });
          DAIJH._assembleElements({
            node: $dv,
            achild: [{
              node: $notification_closeBtn,
              achild: [{ node: $notification__closeBtn_icon }]
            }]
          });
        }

        const removeDv = function () {
          $dv.animate({
            "opacity": 0,
            [positionX]: "-330px"
          }, 300, 'linear', () => {
            const height = parseInt($dv.outerHeight(), 10) + 16;
            const id = $dv.attr("id");
            let notificationList = jq("div[id^='" + idStr + "']");
            let index = 0;
            notificationList.each((i, el) => {
              if (jq(el).attr("id") == id) {
                index = i;
                return false;
              }
            });
            $dv.remove();
            notificationList.slice(index).each((i, el) => {
              if (jq(el).attr("id") != id) {
                jq(el).css({ [positionY]: parseInt(jq(el).css(positionY), 10) - height })
              }
            });
            if (callback) {
              callback();
            }
          });
        };

        DAIJH._assembleElements({
          node: $dv,
          achild: [{
            node: $notification_group,
            achild: [
              { node: $notification_title },
              { node: $notification_content }
            ]
          }]
        });

        $dv.appendTo(jq("body")).animate({ ...positionShowStyle }, 300);

        if (duration > 0) {
          setTimeout(function () {
            if (DAIJH._isExist(jq("#" + id))) { removeDv(); }
          }, duration);
        }
      }
    }
  });
})(jQuery);